package com.jiedeng.controller;

import com.jiedeng.dao.*;
import com.jiedeng.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 覃远龙
 * @ClassName: MultiController
 * @Description:
 * @date 2017/4/24 10:09
 */
@RestController
@RequestMapping("/multi")
public class MultiController{

    @Autowired
    IPeriodRepository periodRepositoty;

    @Autowired
    IPositionRepository positionRepository;

    @Autowired
    IWBSRepository iwbsRepository;

    @Autowired
    IObsRepository obsRepository;

    @Autowired
    IEpsRepository epsRepository;

    @Autowired
    IZoneRepository zoneRepository;

    @Autowired
    ISysFileFolderRepository sysFileFolderRepository;

    //---------------- period table  -----------------//
    @RequestMapping(value = "allPeriods", method = RequestMethod.GET)
    public List<Period> findAllPeriods(){
        return periodRepositoty.findAll();
    }


    @RequestMapping(value = "period/all", method = RequestMethod.GET)
    public List<PeriodTree> findAllParents(){
        List<PeriodTree> treesParent = new ArrayList<PeriodTree>();
        List<Period> periods = periodRepositoty.findByParent(0);
        for (Period p : periods) {
            PeriodTree node = new PeriodTree();
            node.setPeriod(p);
            getPeriodChildren(node);
            treesParent.add(node);
        }

        return treesParent;
    }

    private void getPeriodChildren(PeriodTree p){
        List<PeriodTree> treesParent = new ArrayList<PeriodTree>();
        List<Period> periods = periodRepositoty.findByParent(p.getPeriod().getId());
        for (Period period : periods) {
            PeriodTree treeNode = new PeriodTree();
            treeNode.setPeriod(period);
            treesParent.add(treeNode);
            getPeriodChildren(treeNode);
        }
        p.setChildren(treesParent);
    }

    private void deletePeriodChildren(long id){
        List<Period> periods = periodRepositoty.findByParent(id);
        for (Period p: periods) {
            periodRepositoty.deleteById(p.getId());
            deletePeriodChildren(p.getId());
        }
    }

    @RequestMapping(value = "period/save")
    public Period savePeriod(@RequestBody Period Period){
        return periodRepositoty.save(Period);
    }

    @RequestMapping(value = "period/delete")
    public void deletePeriod(long id){
        periodRepositoty.deleteById(id);
        deletePeriodChildren(id);
    }

    //---------------- position table  -----------------//
    @RequestMapping(value = "position/all")
    public List<PositionTree> findAllPositonParents(){
        List<PositionTree> treesParent = new ArrayList<PositionTree>();
        List<Position> positions = positionRepository.findByParent(0);
        for (Position p : positions) {
            PositionTree node = new PositionTree();
            node.setPosition(p);
            getPositionChildren(node);
            treesParent.add(node);
        }

        return treesParent;
    }

    private void getPositionChildren(PositionTree p){
        List<PositionTree> treesParent = new ArrayList<PositionTree>();
        List<Position> positions = positionRepository.findByParent(p.getPosition().getId());
        for (Position position : positions) {
            PositionTree treeNode = new PositionTree();
            treeNode.setPosition(position);
            treesParent.add(treeNode);
            getPositionChildren(treeNode);
        }
        p.setChildren(treesParent);
    }

    private void deletePositionChildren(long id){
        List<Position> positions = positionRepository.findByParent(id);
        for (Position p: positions) {
            positionRepository.deleteById(p.getId());
            deletePositionChildren(p.getId());
        }
    }

    @RequestMapping(value = "position/save")
    public Position savePosition(@RequestBody Position position){
        return positionRepository.save(position);
    }

    @RequestMapping(value = "position/delete")
    public void deletePosition(long id){
        positionRepository.deleteById(id);
        deletePositionChildren(id);
    }

    //---------------- wbs table  -----------------//
    @RequestMapping(value = "wbs/all")
    public List<WBSTree> findAllWBSParents(){
        List<WBSTree> treesParent = new ArrayList<WBSTree>();
        List<WBS> wbs = iwbsRepository.findByParent(0);
        for (WBS p : wbs) {
            WBSTree node = new WBSTree();
            node.setWbs(p);
            getWBSChildren(node);
            treesParent.add(node);
        }

        return treesParent;
    }

    private void getWBSChildren(WBSTree p){
        List<WBSTree> treesParent = new ArrayList<WBSTree>();
        List<WBS> WBSs = iwbsRepository.findByParent(p.getWbs().getId());
        for (WBS w : WBSs) {
            WBSTree treeNode = new WBSTree();
            treeNode.setWbs(w);
            treesParent.add(treeNode);
            getWBSChildren(treeNode);
        }
        p.setChildren(treesParent);
    }

    private void deleteWBSChildren(long id){
        List<WBS> WBSs = iwbsRepository.findByParent(id);
        for (WBS p: WBSs) {
            iwbsRepository.deleteById(p.getId());
            deleteWBSChildren(p.getId());
        }
    }

    @RequestMapping(value = "wbs/save")
    public WBS saveWBS(@RequestBody WBS wbs){
        return iwbsRepository.save(wbs);
    }

    @RequestMapping(value = "wbs/delete")
    public void deleteWBS(long id){
        iwbsRepository.deleteById(id);
        deleteWBSChildren(id);
    }

    //---------------- eps table  -----------------//
    @RequestMapping(value = "eps/list",method = RequestMethod.GET)
    public  List<EPS> findAllEps(){
        return  epsRepository.findAll();
    }

    @RequestMapping(value = "eps/all")
    public List<EpsTree> findAllEPSParents(){
        List<EpsTree> treesParent = new ArrayList<EpsTree>();
        List<EPS> eps = epsRepository.findByParent(1);
        for (EPS p : eps) {
            EpsTree node = new EpsTree();
            node.setEps(p);
            getEPSChildren(node);
            treesParent.add(node);
        }

        return treesParent;
    }

    private void getEPSChildren(EpsTree p){
        List<EpsTree> treesParent = new ArrayList<EpsTree>();
        List<EPS> EPSs = epsRepository.findByParent(p.getEps().getId());
        for (EPS w : EPSs) {
            EpsTree treeNode = new EpsTree();
            treeNode.setEps(w);
            treesParent.add(treeNode);
            getEPSChildren(treeNode);
        }
        p.setChildren(treesParent);
    }

    private void deleteEPSChildren(long id){
        List<EPS> EPSs = epsRepository.findByParent(id);
        for (EPS p: EPSs) {
            epsRepository.deleteById(p.getId());
            deleteEPSChildren(p.getId());
        }
    }

    @RequestMapping(value = "eps/save")
    public EPS saveEPS(@RequestBody EPS eps){
        return epsRepository.save(eps);
    }

    @RequestMapping(value = "eps/delete")
    public void deleteEPS(long id){
        epsRepository.deleteById(id);
        deleteEPSChildren(id);
    }

    //---------------- obs table  -----------------//

    @RequestMapping(value = "obs/list")
    public List<OBS> findAllOBS(){
//        List<OBS> obsList = new ArrayList<OBS>();
//        obsList = obsRepository.findByParent(0);
//        for(OBS obs : obsList){
//            setObsChildren(obs);
//        }
//        return obsList;

        return  obsRepository.findAll();
    }

    public void setObsChildren(OBS obs){
        List<OBS> obsList = obsRepository.findByParent(obs.getId());
        obs.setChildren(obsList);
        for(OBS o : obsList){
            setObsChildren(o);
        }
    }

    @RequestMapping(value = "obs/all")
    public List<ObsTree> findAllOBSParents(){
        List<ObsTree> treesParent = new ArrayList<ObsTree>();
        List<OBS> obs = obsRepository.findByParent(0);
        for (OBS p : obs) {
            ObsTree node = new ObsTree();
            node.setObs(p);
            getOBSChildren(node);
            treesParent.add(node);
        }

        return treesParent;
    }

    private void getOBSChildren(ObsTree p){
        List<ObsTree> treesParent = new ArrayList<ObsTree>();
        List<OBS> OBSs = obsRepository.findByParent(p.getObs().getId());
        for (OBS w : OBSs) {
            ObsTree treeNode = new ObsTree();
            treeNode.setObs(w);
            treesParent.add(treeNode);
            getOBSChildren(treeNode);
        }
        p.setChildren(treesParent);
    }

    private void deleteOBSChildren(long id){
        List<OBS> OBSs = obsRepository.findByParent(id);
        for (OBS p: OBSs) {
            obsRepository.deleteById(p.getId());
            deleteOBSChildren(p.getId());
        }
    }

    @RequestMapping(value = "obs/save")
    public OBS saveOBS(@RequestBody OBS obs){
        return obsRepository.save(obs);
    }

    @RequestMapping(value = "obs/delete")
    public void deleteOBS(long id){
        obsRepository.deleteById(id);
        deleteOBSChildren(id);
    }

    //---------------- zone table  -----------------//

    @RequestMapping(value = "zone/list", method = RequestMethod.GET)
    public List<Zone> findZones(){
        return zoneRepository.findAll();
    }

    @RequestMapping(value = "zone/all")
    public List<ZoneTree> findAllZoneParents(){
        List<ZoneTree> zonesParent = new ArrayList<ZoneTree>();
        List<Zone> zones = zoneRepository.findByParent(1);
        for (Zone p : zones) {
            ZoneTree node = new ZoneTree();
            node.setZone(p);
            getZoneChildren(node);
            zonesParent.add(node);
        }

        return zonesParent;
    }

    private void getZoneChildren(ZoneTree p){
        List<ZoneTree> treesParent = new ArrayList<ZoneTree>();
        List<Zone> zones = zoneRepository.findByParent(p.getZone().getId());
        for (Zone w : zones) {
            ZoneTree treeNode = new ZoneTree();
            treeNode.setZone(w);
            treesParent.add(treeNode);
            getZoneChildren(treeNode);
        }
        p.setChildren(treesParent);
    }

    private void deleteZoneChildren(long id){
        List<Zone> zones = zoneRepository.findByParent(id);
        for (Zone p: zones) {
            zoneRepository.deleteById(p.getId());
            deleteZoneChildren(p.getId());
        }
    }

    @RequestMapping(value = "zone/save")
    public Zone saveZone(@RequestBody Zone zone){
        return zoneRepository.save(zone);
    }

    @RequestMapping(value = "zone/delete")
    public void deleteZone(long id){
        zoneRepository.deleteById(id);
        deleteZoneChildren(id);
    }


    //---------------- sysfilefolder table  -----------------//

    @RequestMapping(value = "filefolder/list", method = RequestMethod.GET)
    public List<SysFileFolder> findFileFolders(){
        return sysFileFolderRepository.findAll();
    }

    @RequestMapping(value = "filefolder/all")
    public List<SysFileFolderTree> findAllFileParents(){
        List<SysFileFolderTree> zonesParent = new ArrayList<SysFileFolderTree>();
        List<SysFileFolder> zones = sysFileFolderRepository.findByParent(0);
        for (SysFileFolder p : zones) {
            SysFileFolderTree node = new SysFileFolderTree();
            node.setSysFileFolder(p);
            getSysFileChildren(node);
            zonesParent.add(node);
        }

        return zonesParent;
    }

    private void getSysFileChildren(SysFileFolderTree p){
        List<SysFileFolderTree> treesParent = new ArrayList<SysFileFolderTree>();
        List<SysFileFolder> zones = sysFileFolderRepository.findByParent(p.getSysFileFolder().getId());
        for (SysFileFolder w : zones) {
            SysFileFolderTree treeNode = new SysFileFolderTree();
            treeNode.setSysFileFolder(w);
            treesParent.add(treeNode);
            getSysFileChildren(treeNode);
        }
        p.setChildren(treesParent);
    }

    private void deleteSysFileChildren(long id){
        List<SysFileFolder> zones = sysFileFolderRepository.findByParent(id);
        for (SysFileFolder p: zones) {
            sysFileFolderRepository.deleteById(p.getId());
            deleteZoneChildren(p.getId());
        }
    }

    @RequestMapping(value = "filefolder/save")
    public SysFileFolder saveFileFolder(@RequestBody SysFileFolder zone){
        return sysFileFolderRepository.save(zone);
    }

    @RequestMapping(value = "filefolder/delete")
    public void deleteFileFolder(long id){
        sysFileFolderRepository.deleteById(id);
        deleteSysFileChildren(id);
    }
}

